package com.lehecai.core.warning;

import com.lehecai.core.api.AbstractApiResultBean;
import com.lehecai.core.util.CoreDateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by qatang on 14-7-22.
 */
public class WarningMessage extends AbstractApiResultBean implements Serializable {

    private static final long serialVersionUID = -3063216150803378375L;

    private WarningType warningType;
    private String subject;
    private String content;
    private Date timestamp;
    private int priority;

    private WarningMethod warningMethod;
    private List<String> receivers;

    public static WarningMessage convertFromJSONObject(JSONObject object) {
        if (object == null) {
            return null;
        }
        WarningMessage warningMessage = new WarningMessage();

        warningMessage.setWarningType(WarningType.getItem(getInt(object, "warning_type")));
        warningMessage.subject = getString(object, "subject");
        warningMessage.content = getString(object, "content");
        warningMessage.setTimestamp(getDate(object, "timestamp", CoreDateUtils.DATETIME));
        warningMessage.priority = getInt(object, "priority");

        if (object.containsKey("warning_method")) {
            warningMessage.setWarningMethod(WarningMethod.getItem(getInt(object, "warning_method")));
        } else {
            warningMessage.setWarningMethod(WarningMethod.MAIL);
        }

        if (object.containsKey("receivers")) {
            JSONArray jsonArray = getArray(object, "receivers");
            if (jsonArray != null && jsonArray.size() > 0) {
                List<String> list = new ArrayList<String>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    list.add(jsonArray.getString(i));
                }
                warningMessage.setReceivers(list);
            } else {
                warningMessage.receivers = null;
            }
        }

        return warningMessage;
    }

    @SuppressWarnings("unchecked")
    public static List<WarningMessage> convertFromJSONArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        List<WarningMessage> list = new ArrayList<WarningMessage>();
        for (Iterator iterator = array.iterator(); iterator.hasNext();) {
            JSONObject object = (JSONObject) iterator.next();
            list.add(convertFromJSONObject(object));
        }
        return list;
    }

    public WarningType getWarningType() {
        return warningType;
    }

    public void setWarningType(WarningType warningType) {
        this.warningType = warningType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public WarningMethod getWarningMethod() {
        return warningMethod;
    }

    public void setWarningMethod(WarningMethod warningMethod) {
        this.warningMethod = warningMethod;
    }

    public List<String> getReceivers() {
        return receivers;
    }

    public void setReceivers(List<String> receivers) {
        this.receivers = receivers;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
}
